<template>
  <div class="shoprule">
    规格搜索:
    <el-select
      v-model="value9"
      multiple
      filterable
      remote
      reserve-keyword
      placeholder="请输入关键词"
      :remote-method="remoteMethod"
      :loading="loading"
    >
      <el-option
        v-for="item in options4"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      >
      </el-option>
    </el-select>
    

    <p class="table_top_btn">
      <el-button type="text" @click="dialogFormVisible = true"
        >添加商品规格</el-button
      >

      <el-dialog title="属性规格" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="规格名称" :label-width="formLabelWidth">
            <el-input
              v-model="form.name"
              auto-complete="off"
              placeholder="请输入标题名称"
            ></el-input>
          </el-form-item>

          <el-form-item label="规格" :label-width="formLabelWidth">
            <el-input
              v-model="form.name"
              auto-complete="off"
              placeholder="请输入规格"
            ></el-input>
          </el-form-item>
          <el-form-item label="规格值" :label-width="formLabelWidth">
            <el-input
              v-model="form.name"
              auto-complete="off"
              placeholder="请输入规格值"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogToVisible = true">取 消</el-button>
          <el-button type="text" @click="dialogToVisible = true"
            >确 定</el-button
          >
        </div>
      </el-dialog>
      <el-dialog title="属性规格" :visible.sync="dialogToVisible">
        <el-form :model="form">
          <el-form-item label="规格名称" :label-width="formLabelWidth">
            <el-input
              v-model="form.name"
              auto-complete="off"
              placeholder="请输入标题名称"
            ></el-input>
          </el-form-item>
        </el-form>
        <p>消耗</p>
        <p class="but_btn">
          <el-button>500W</el-button>
          <el-button>+添加</el-button>
        </p>

        <el-button type="primary">添加新规格</el-button>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogToVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogToVisible = false"
            >确 定</el-button
          >
        </div>
      </el-dialog>

      <el-button>批量删除</el-button>
    </p>

    <div class="table">
      <el-table :data="tableData" border style="width: 100%;">
        
        <el-table-column type="selection" width="66"> </el-table-column>
        <el-table-column prop="id" label="ID" width="160"> </el-table-column>
        <el-table-column prop="name" label="规格名称" width="280">
        </el-table-column>
        <el-table-column prop="title" label="商品规格" width="410">
        </el-table-column>
        <el-table-column prop="have" label="商品属性" width="520">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            > -->
            <el-button type="text" @click="dialogInVisible = true"
              >编辑</el-button
            >

            <el-dialog title="属性规格" :visible.sync="dialogInVisible">
              <el-form :model="form">
                <el-form-item label="规格名称" :label-width="formLabelWidth">
                  <el-input
                    v-model="form.name"
                    auto-complete="off"
                    placeholder="请输入标题名称"
                  ></el-input>
                </el-form-item>

                <el-form-item label="规格" :label-width="formLabelWidth">
                  <el-input
                    v-model="form.name"
                    auto-complete="off"
                    placeholder="请输入规格"
                  ></el-input>
                </el-form-item>
                <el-form-item label="规格值" :label-width="formLabelWidth">
                  <el-input
                    v-model="form.name"
                    auto-complete="off"
                    placeholder="请输入规格值"
                  ></el-input>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogInVisible = false">取 消</el-button>
                <el-button type="text" @click="dialogInVisible = false"
                  >确 定</el-button
                >
              </div>
            </el-dialog>
            <el-button
              @click.native.prevent="deleteRow(scope.$index, tableData)"
              type="text"
              size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-pagination
      background=""
      layout="prev, pager, next"
      :total="total"
      :page-size="pageSize"
      @current-change="pageChange"
    ></el-pagination>
  </div>
</template>
<script>
export default {
  data() {
    return {
      search: "",
      searchData: "",
      tableData: [],

      options4: [],
      value9: [],
      list: [],
      loading: false,
      states: [],

      dialogFormVisible: false,
      dialogToVisible: false,
      dialogInVisible: false,
      form: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: ""
      },
      formLabelWidth: "120px",
      pageIndex: 1,
      pageSize: 10,
      total: 0
    };
  },
  created() {
    // this.$api.ruleOne().then(res => {
    //   console.log(res);
    //   this.tableData = res.data.list.slice(0, 5);
    //   console.log(this.tableData, "1");
    // });
    this.pageChange(1);
  },

  mounted() {
    this.list = this.states.map(item => {
      return { value: item, label: item };
    });
  },
  methods: {
    handleEdit(index, row) {
      console.log(index, row);
    },
    deleteRow(index, rows) {
      rows.splice(index, 1);
    },
    remoteMethod(query) {
      if (query !== "") {
        this.loading = true;
        setTimeout(() => {
          this.loading = false;
          this.options4 = this.list.filter(item => {
            return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
          });
        }, 200);
      } else {
        this.options4 = [];
      }
    },
    async pageChange(ind) {
      this.pageIndex = ind;
      this.$api
        .ruleOne({
          pageIndex: this.pageIndex,
          pageSize: this.pageSize
        })
        .then(res => {
          if (res.code === 1) {
            this.tableData = res.data.list;
            this.total = res.data.total;
          } else {
            // 抛出包错
          }
        });
    },
    handleClick(tab, event) {
      console.log(tab, event);
    }
  }
};
</script>
<style lang="scss" scoped>
.shoprule{
  background: #fff;
  padding: 20px;
}
.table_top_btn {
  margin: 20px 0;
  button{
    margin: 0 10px;
  }
}
.table {
  width: 100%;
  margin: 30px 0;
}

.but_btn {
  margin: 20px 0;
}
</style>

